package com.java8.Lambda;

import org.junit.Test;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

@FunctionalInterface
interface MyMath {
    int operate(int a, int b);
}
public class demo03_与函数式接口结合 {
    public void test(MyMath m) {
        int result = m.operate(20, 10);
        System.out.println(result);
    }
    @Test
    public void testdemo01() {
        // 使用 Lambda 简化调用
        test((a,b)->a+b);
        test((a,b)->a-b);
        test((a,b)->a*b);
        test((a,b)->a/b);
    }
    @Test
    public void testdemo02(){
        String str="AA,hello world";
        //差不多Lambda表达式重写了函数式接口的apply方法
        //传参返回
        Function<String,Integer> f= s->s.length();
        //传参不返回
        Consumer<String> c= s-> System.out.println(s);
        //传参，返回boolean
        Predicate<String> p= s->s.startsWith("A");
        //不传参，返回
        Supplier<Double> s=()->Math.random();


        //调用执行
        System.out.println(f.apply(str));//f.apply(str) 相当与s.length()
        c.accept(str);
        System.out.println(p.test(str));
        System.out.println(s.get());
    }
}
